﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace libSyllabus.Model
{
    /// <summary>
    /// This is Course entity class, which represents an entry in the Courses table.
    /// </summary>
    /// <remarks>
    /// For now, Courses table contains following columns:
    ///  - ID               : Auto Generated Index (Number)
    ///  - SemesterID   : ID of the parent Semester (Number)
    ///  - Acronym       : Abbreviated Course Name, for e.g. "JPNS101" (String)
    ///  - Title           : Full Course Name, for e.g. "Japanese Level I" (String)
    ///  
    /// Property Change Notification is taken care of in the base class
    /// </remarks>
    [Table(Name = "Courses")]
    public sealed class CourseEntity : EntityBase
    {
        //Note: Database relations not added yet

        #region Constants
        
        // Column Name Constants
        public const string COURSE_ID = "ID";
        public const string COURSE_SEMESTERID = "SemesterID";
        private const string COURSE_ACRONYM = "Acronym";
        private const string COURSE_NAME = "Title";

        #endregion

        #region Constructor

        public CourseEntity()
        {
            m_classes = new EntitySet<ClassEntity>();
            m_sections = new EntitySet<SectionEntity>();
            m_semester = new EntityRef<SemesterEntity>();
        }

        #endregion

        #region Fields

        // Fields mapped to the database
        [Column(Name = COURSE_ID, IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false,
            DbType = "INT NOT NULL Identity", AutoSync = AutoSync.OnInsert)]
        private Int32 m_id;

        [Column(Name = COURSE_SEMESTERID, CanBeNull = false)]
        private Int32 m_semesterID;

        [Column(Name = COURSE_ACRONYM, CanBeNull = false)]
        private String m_acronym;

        [Column(Name = COURSE_NAME, CanBeNull = false)]
        private String m_name;

        // One-to-one relationship to parent semester
        EntityRef<SemesterEntity> m_semester;

        // One-to-many relationship to child sections
        EntitySet<SectionEntity> m_sections;

        // One-to-many relationship to child classes
        EntitySet<ClassEntity> m_classes;

        #endregion

        #region Properties

        public Int32 ID
        {
            get
            {
                return m_id;
            }
        }

        public Int32 SemesterID
        {
            get
            {
                return m_semesterID;
            }
        }

        public String Acronym
        {
            get
            {
                return m_acronym;
            }
            set
            {
                if (m_acronym != value)
                {
                    RaisePropertyChanging(COURSE_ACRONYM);
                    m_acronym = value;
                    RaisePropertyChanged(COURSE_ACRONYM);
                }
            }
        }

        public String Title
        {
            get
            {
                return m_name;
            }
            set
            {
                if (m_name != value)
                {
                    RaisePropertyChanging(COURSE_NAME);
                    m_name = value;
                    RaisePropertyChanged(COURSE_NAME);
                }
            }
        }

        [Association(Storage = "m_semester", ThisKey = COURSE_SEMESTERID)]
        public SemesterEntity Semester
        {
            get
            {
                return m_semester.Entity;
            }
        }

        [Association(Storage = "m_sections", OtherKey = SectionEntity.SECTION_COURSEID)]
        public EntitySet<SectionEntity> Sections
        {
            get
            {
                return m_sections;
            }
        }

        [Association(Storage = "m_classes", OtherKey = ClassEntity.CLASS_COURSEID)]
        public EntitySet<ClassEntity> Classes
        {
            get
            {
                return m_classes;
            }
        }

        #endregion
    }
}
